package com.smart.mac.common;

import java.io.Serializable;

public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    final static Integer SUCCESS = 0;
    final static Integer FAIL = 1;
    private int code;
    private String msg;
    private T data;

    private Object errorData;


    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, (String)null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> R<T> ok(String msg) {
        return restResult(null, SUCCESS, msg);
    }


    public static <T> R<T> failed(T data) {
        return restResult(data, FAIL, (String)null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, FAIL, msg);
    }


    public static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public static Object errorDate(Object errorData, int code, String msg) {
        R<Object> apiResult = new R();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        apiResult.setErrorData(errorData);
        return apiResult;
    }

    @Override
    public String toString() {
        return "R(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ", errorData=" + this.getErrorData() + ")";
    }

    public R() {
    }

    public R(int code, String msg, T data, Object errorData) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.errorData = errorData;
    }

    public int getCode() {
        return this.code;
    }

    public R<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    public R<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public R<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Object getErrorData() {
        return this.errorData;
    }

    public R<T> setErrorData(Object errorData) {
        this.errorData = errorData;
        return this;
    }
}
